package com.youkeyi.ddy.cloud.application.business.storage.vaccinum.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.BusinessVaccinumCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.dto.BusinessVaccinumListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.dto.BusinessVaccinumListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.param.BusinessVaccinumCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.param.BusinessVaccinumDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.param.BusinessVaccinumListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.vaccinum.param.BusinessVaccinumUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.StorageVaccinumCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.dto.StorageVaccinumListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * StorageVaccinumController
 *
 * @Author liuxiawang
 * @Date 2024/3/25 10:54 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessVaccinumController implements BusinessVaccinumCloudService {
    
    @Autowired
    private StorageVaccinumCloudService storageVaccinumCloudService;

    @Override
    public BusinessVaccinumListCloudDto list(BusinessVaccinumListCloudParam param) {
        BusinessVaccinumListCloudDto dto = new BusinessVaccinumListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        StorageVaccinumQueryCloudParam vaccinumQueryCloudParam = new StorageVaccinumQueryCloudParam();
        vaccinumQueryCloudParam.setVaccinumPrimaryKeys(param.getVaccinumPrimaryKeys());
        vaccinumQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        vaccinumQueryCloudParam.setKeyWordFuzzy(param.getKeyWordFuzzy());
        vaccinumQueryCloudParam.setVaccinationPopulations(param.getVaccinationPopulations());
        vaccinumQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        vaccinumQueryCloudParam.setPageSize(param.getPageSize());
        vaccinumQueryCloudParam.setCurrentPage(param.getCurrentPage());
        StorageVaccinumListCloudDto vaccinumDtos = storageVaccinumCloudService.query(vaccinumQueryCloudParam);
        if (null != vaccinumDtos && CollectionUtils.isNotEmpty(vaccinumDtos.getRows())) {
            List<BusinessVaccinumListItemCloudDto> items = new ArrayList<>();
            vaccinumDtos.getRows().forEach(vaccinum -> {
                BusinessVaccinumListItemCloudDto item = new BusinessVaccinumListItemCloudDto();
                item.setVaccinumPrimaryKey(vaccinum.getVaccinumPrimaryKey());
                item.setName(vaccinum.getName());
                item.setAliasName(vaccinum.getAliasName());
                item.setKeyWord(vaccinum.getKeyWord());
                item.setCategoryKey(vaccinum.getCategoryKey());
                item.setCategoryName(vaccinum.getCategoryName());
                item.setVaccinationPopulation(vaccinum.getVaccinationPopulation());
                item.setVaccinationMethod(vaccinum.getVaccinationMethod());
                item.setPrices(vaccinum.getPrices());
                item.setCost(vaccinum.getCost());
                item.setSaveEnvironment(vaccinum.getSaveEnvironment());
                item.setValidityPeriod(vaccinum.getValidityPeriod());
                item.setVaccinationFrequency(vaccinum.getVaccinationFrequency());
                item.setPlanOne(vaccinum.getPlanOne());
                item.setPlanTwo(vaccinum.getPlanTwo());
                item.setPlanThree(vaccinum.getPlanThree());
                item.setPlanFour(vaccinum.getPlanFour());
                item.setPlanFive(vaccinum.getPlanFive());
                item.setBarCode(vaccinum.getBarCode());
                item.setQrCode(vaccinum.getQrCode());
                item.setThirdPartyIdentificationKey(vaccinum.getThirdPartyIdentificationKey());
                item.setIdentification(vaccinum.getIdentification());
                item.setPath(vaccinum.getPath());
                item.setManufacturer(vaccinum.getManufacturer());
                item.setProductionLicense(vaccinum.getProductionLicense());
                item.setRemark(vaccinum.getRemark());
                item.setIsDeleted(vaccinum.getIsDeleted());
                item.setIsDeletedName(vaccinum.getIsDeletedName());
                item.setCreateUserKey(vaccinum.getCreateUserKey());
                item.setCreateUserName(vaccinum.getCreateUserName());
                item.setCreateTime(vaccinum.getCreateTime());
                item.setUpdateUserKey(vaccinum.getUpdateUserKey());
                item.setUpdateUserName(vaccinum.getUpdateUserName());
                item.setUpdateTime(vaccinum.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(vaccinumDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessVaccinumCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageVaccinumCreateCloudParam vaccinumCreateCloudParam = new StorageVaccinumCreateCloudParam();
        vaccinumCreateCloudParam.setCorpKey(corpKey);
        vaccinumCreateCloudParam.setCorpKey(corpKey);
        vaccinumCreateCloudParam.setName(param.getName());
        vaccinumCreateCloudParam.setAliasName(param.getAliasName());
        vaccinumCreateCloudParam.setKeyWord(param.getKeyWord());
        vaccinumCreateCloudParam.setCategoryKey(param.getCategoryKey());
        vaccinumCreateCloudParam.setCategoryName(param.getCategoryName());
        vaccinumCreateCloudParam.setVaccinationPopulation(param.getVaccinationPopulation());
        vaccinumCreateCloudParam.setVaccinationMethod(param.getVaccinationMethod());
        vaccinumCreateCloudParam.setPrices(param.getPrices());
        vaccinumCreateCloudParam.setCost(param.getCost());
        vaccinumCreateCloudParam.setSaveEnvironment(param.getSaveEnvironment());
        vaccinumCreateCloudParam.setValidityPeriod(param.getValidityPeriod());
        vaccinumCreateCloudParam.setVaccinationFrequency(param.getVaccinationFrequency());
        vaccinumCreateCloudParam.setPlanOne(param.getPlanOne());
        vaccinumCreateCloudParam.setPlanTwo(param.getPlanTwo());
        vaccinumCreateCloudParam.setPlanThree(param.getPlanThree());
        vaccinumCreateCloudParam.setPlanFour(param.getPlanFour());
        vaccinumCreateCloudParam.setPlanFive(param.getPlanFive());
        vaccinumCreateCloudParam.setBarCode(param.getBarCode());
        vaccinumCreateCloudParam.setQrCode(param.getQrCode());
        vaccinumCreateCloudParam.setThirdPartyIdentificationKey(param.getThirdPartyIdentificationKey());
        vaccinumCreateCloudParam.setIdentification(param.getIdentification());
        vaccinumCreateCloudParam.setPath(param.getPath());
        vaccinumCreateCloudParam.setManufacturer(param.getManufacturer());
        vaccinumCreateCloudParam.setProductionLicense(param.getProductionLicense());
        vaccinumCreateCloudParam.setRemark(param.getRemark());
        vaccinumCreateCloudParam.setRemark(param.getRemark());
        vaccinumCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccinumCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return storageVaccinumCloudService.create(vaccinumCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessVaccinumUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getVaccinumPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageVaccinumUpdateCloudParam vaccinumUpdateCloudParam = new StorageVaccinumUpdateCloudParam();
        vaccinumUpdateCloudParam.setVaccinumPrimaryKey(param.getVaccinumPrimaryKey());
        vaccinumUpdateCloudParam.setCorpKey(corpKey);
        vaccinumUpdateCloudParam.setName(param.getName());
        vaccinumUpdateCloudParam.setAliasName(param.getAliasName());
        vaccinumUpdateCloudParam.setKeyWord(param.getKeyWord());
        vaccinumUpdateCloudParam.setCategoryKey(param.getCategoryKey());
        vaccinumUpdateCloudParam.setCategoryName(param.getCategoryName());
        vaccinumUpdateCloudParam.setVaccinationPopulation(param.getVaccinationPopulation());
        vaccinumUpdateCloudParam.setVaccinationMethod(param.getVaccinationMethod());
        vaccinumUpdateCloudParam.setPrices(param.getPrices());
        vaccinumUpdateCloudParam.setCost(param.getCost());
        vaccinumUpdateCloudParam.setSaveEnvironment(param.getSaveEnvironment());
        vaccinumUpdateCloudParam.setValidityPeriod(param.getValidityPeriod());
        vaccinumUpdateCloudParam.setVaccinationFrequency(param.getVaccinationFrequency());
        vaccinumUpdateCloudParam.setPlanOne(param.getPlanOne());
        vaccinumUpdateCloudParam.setPlanTwo(param.getPlanTwo());
        vaccinumUpdateCloudParam.setPlanThree(param.getPlanThree());
        vaccinumUpdateCloudParam.setPlanFour(param.getPlanFour());
        vaccinumUpdateCloudParam.setPlanFive(param.getPlanFive());
        vaccinumUpdateCloudParam.setBarCode(param.getBarCode());
        vaccinumUpdateCloudParam.setQrCode(param.getQrCode());
        vaccinumUpdateCloudParam.setThirdPartyIdentificationKey(param.getThirdPartyIdentificationKey());
        vaccinumUpdateCloudParam.setIdentification(param.getIdentification());
        vaccinumUpdateCloudParam.setPath(param.getPath());
        vaccinumUpdateCloudParam.setManufacturer(param.getManufacturer());
        vaccinumUpdateCloudParam.setProductionLicense(param.getProductionLicense());
        vaccinumUpdateCloudParam.setRemark(param.getRemark());
        vaccinumUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccinumUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return storageVaccinumCloudService.update(vaccinumUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessVaccinumDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getVaccinumPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageVaccinumDeleteCloudParam vaccinumDeleteCloudParam = new StorageVaccinumDeleteCloudParam();
        vaccinumDeleteCloudParam.setVaccinumPrimaryKeys(param.getVaccinumPrimaryKeys());
        vaccinumDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccinumDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return storageVaccinumCloudService.delete(vaccinumDeleteCloudParam);
    }
}
